<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="org.wso2.carbon.registry.common.ui.UIException" %>
<%@ page import="org.wso2.carbon.registry.custom.skb.ui.utils.AddKBUtil" %>
<%@ page import="org.wso2.carbon.registry.custom.skb.ui.clients.TopicServiceClient" %>
<%@ page import="java.util.ArrayList"  %>

<%
//    String parentPath = request.getParameter("parentPath");
    String parentPath = "/SupportKB/Problem";
    String problemName = request.getParameter("problemName");
    String sPath = "/SupportKB/Problem/"+problemName;
//    String sPath = parentPath +"/"+ problemName;

//    String problemContent="<problem></pro>";

    String description = request.getParameter("description");
    String link = request.getParameter("link");
    String question = request.getParameter("qnArray");
    System.out.println("Q[] :" +question);
    String qn[] = question.split("###,");
    String answer = request.getParameter("ansArray");
    System.out.println("A[] :" +answer);
    String ans[] = answer.split("###,");
    String solution = request.getParameter("solArray");
    System.out.println("SOL[] :" +solution);
    String sol[] = solution.split("###,");
    String product = request.getParameter("product");
    System.out.println("Product :"+product);
    String productRelease = request.getParameter("productRelease");
    System.out.println("PR :"+productRelease);
    String carbonPlatform = request.getParameter("carbonPlatform");
    System.out.println("CP :"+carbonPlatform);
    String jdkPlatform = request.getParameter("jdkPlatform");
    System.out.println("JDK :"+jdkPlatform);

    String keyWord = request.getParameter("keyWord");



//    String problemContent = "<problem problemName=\""+problemName+"\" description=\""+description+"\" link=\""+link+"\"><questions question=\""+question+"\" /><answers answer=\""+answer+"\" /><solutions solution=\""+answer+"\" /><products product=\""+product+"\" productRelease=\""+productRelease+"\" /><platform carbonPlatform=\""+carbonPlatform+"\" jdkPlatform=\""+jdkPlatform+"\" /><keyWords keyWord=\""+keyWord+"\" /></problem>";

   
    try {
        AddKBUtil.addKBItem(request, config, session);
        TopicServiceClient client = new TopicServiceClient(config, session);
            client.addCollection("/SupportKB");
            client.addCollection("/SupportKB/Problem");
            client.addCollection("/SupportKB/Question");
            client.addCollection("/SupportKB/Answer");
            client.addCollection("/SupportKB/Solution");
            client.addCollection("/Platform/Carbon_Platform");
            client.addCollection("/Platform/JDK_Platform");
        if(link != null && link != ""){
            client.addAssociation(sPath, link, "Issue Link");
        }

        
        
//        client.addProblemResource("/SupportKB/Problem/"+problemName, list(problemName,description,link,question,answer,solution,product,productRelease,carbonPlatform,jdkPlatform,keyWord) );
//        client.addCollection("/WSO2Products/Product");
//        client.addCollection("/WSO2Products/Product_Release");
        for(int i=0;i<qn.length;i++){
            qn[i] = qn[i].replace("###","");
            ans[i] = ans[i].replace("###","");
            if(qn[i] != null && !(qn[i].equals("")) && ans[i]!=null && !(ans[i].equals(""))){
                System.out.println("it was here \n");
                
                client.addResource("/SupportKB/Question/"+qn[i], qn[i]+"\n\n"+ans[i]);
                client.addAssociation(sPath, "/SupportKB/Question/"+qn[i], "Question : "+(i+1));
            }
        }
        for(int i=0;i<ans.length;i++){
            ans[i] = ans[i].replace("###","");
            if(ans[i]!=null && !(ans[i].equals(""))){
                client.addResource("/SupportKB/Answer/"+ans[i], ans[i]);
                client.addAssociation(sPath, "/SupportKB/Answer/"+ans[i], "Answer : "+(i+1));
            }
        }
        for(int i=0;i<sol.length;i++){
            sol[i] = sol[i].replace("###","");
            if(sol[i]!=null && !(sol[i].equals(""))){
                client.addResource("/SupportKB/Solution/"+sol[i], sol[i]);
                client.addAssociation(sPath, "/SupportKB/Solution/"+sol[i], "Solution : "+(i+1));
            }
        }

        if(product != null){
        client.addAssociation(sPath, "/WSO2Products/"+product, "Product");
            if(productRelease != null && !(productRelease.equals(""))){
                client.addCollection("/WSO2Products/"+product+"/Product_Release");
                client.addResource("/WSO2Products/"+product+"/Product_Release/"+productRelease, productRelease);
                client.addAssociation(sPath, "/WSO2Products/"+product+"/Product_Release/"+productRelease, "ProductRelease");
            }
        }

        if(carbonPlatform != null){
            client.addResource("/Platform/Carbon_Platform/"+carbonPlatform ,carbonPlatform);
            client.addAssociation(sPath, "/Platform/Carbon_Platform/"+carbonPlatform, "CarbonPlatform");
        }
        if(jdkPlatform != null){
            client.addResource("/Platform/JDK_Platform/"+jdkPlatform, jdkPlatform);
            client.addAssociation(sPath, "/Platform/JDK_Platform/"+jdkPlatform, "JDKPlatform");
        }
//        System.out.println("Asso Added");
//        client.addKeyWord(sPath, "test tag");
        if(keyWord != null && !(keyWord.equals(""))){
            client.addKeyWord(sPath, keyWord);
        }
//        System.out.println("KeyWord Added");

    } catch (UIException e) {
%>
Could not add endpoint
<%
        return;
    }
//
//    String resourcePagePath = "../../../resources/resource.jsp?path=" + sPath;

    String resourcePagePath = "skb_view_ajaxprocessor.jsp?path=" + sPath;
    response.sendRedirect(resourcePagePath);



               
%>